package com.vitas.coin.util

import android.Manifest
import android.content.Context
import android.content.DialogInterface.OnDismissListener
import android.os.Build
import android.provider.Settings
import android.view.View
import android.view.inputmethod.InputMethodManager
import androidx.fragment.app.FragmentActivity
import com.vitas.coin.data.bean.ChildrenVoiceBean
import com.vitas.coin.data.bean.DownloadedBean
import com.vitas.coin.data.bean.MusicType
import com.vitas.coin.db.DownDao
import com.vitas.coin.db.ListDao
import com.vitas.coin.db.TypeDao
import com.vitas.coin.resp.CollectDaoResp
import com.vitas.coin.vm.BaseMusicVM
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.File

object Utils : BaseMusicVM() {

    suspend fun getList(): ArrayList<MusicType> {
        var list = TypeDao().getAll()
        var set = getFavor()
        for (item in list) {
            item.list = getListContent(item.id, set)
        }
        return list
    }

    suspend fun getContent(id: String): ArrayList<ChildrenVoiceBean> {
        var set = getFavor()
        return getListContent(id, set)
    }

    suspend fun getFavor(): HashSet<Int> {
        var tmp = withContext(Dispatchers.IO) {
            CollectDaoResp.queryAll()
        }
        var set = HashSet<Int>()
        for (item in tmp) {
            set.add(item.id)
        }
        return set
    }

    fun getListContent(id: String, set: HashSet<Int>): ArrayList<ChildrenVoiceBean> {
        var list = ListDao(id).getAll()
        for (item in list) {
            item.isCollect = set.contains(item.id)
        }
        return list
    }

    fun setSound(context: FragmentActivity, file: File, name: String) {
        actionAct = { context }
        if (!Settings.System.canWrite(context)) {
            showApplyPermissionPromptDialog(
                1,
                2,
                ChildrenVoiceBean(name, "file://" + file.absolutePath)
            )
            return
        }
        downloadEnd(file, 2, false, null)
    }

    fun download(act: FragmentActivity, mChildrenVoiceBean: ChildrenVoiceBean, funType: Int) {
        actionAct = { act }
        download(mChildrenVoiceBean, funType)
    }

    fun download(
        act: FragmentActivity,
        mChildrenVoiceBean: ChildrenVoiceBean,
        funType: Int,
        editAct: ((String?, String) -> Unit)? = null,
        listener: OnDismissListener? = null
    ) {
        actionAct = { act }
        download(mChildrenVoiceBean, funType, editAct, listener)
    }

    suspend fun getDownloaded(): MutableList<DownloadedBean> {
        return DownDao().getAll()
    }

    fun checkRight(context: FragmentActivity): Boolean {
        actionAct = { context }
        val requestList = ArrayList<String>()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            requestList.add(Manifest.permission.READ_MEDIA_AUDIO)
            requestList.add(Manifest.permission.READ_MEDIA_VIDEO)
        } else {
            requestList.add(Manifest.permission.READ_EXTERNAL_STORAGE)
            requestList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE)
        }
        var bo = PermissionsUtils.checkPermissionsGroup(
            actionAct.invoke(),
            requestList.toTypedArray()
        )
        if (!bo) {
            showApplyPermissionPromptDialog(
                3,
                bean = ChildrenVoiceBean("a", "a"),
                next = {

                }
            )
        }
        return bo
    }

    fun hideInput(v: View): Boolean {
        val imm =
            v.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        val bo = imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0)
        return bo
    }

    fun showLoadingDialog(
        act: FragmentActivity,
        progressText: String,
        listener: OnDismissListener? = null
    ) {
        actionAct = { act }
        showLoadingDialog(progressText, listener)
    }
}
